package com.fl.nj.service.impl;

import com.fl.nj.entity.ServerService;
import com.fl.nj.mapper.ServerServiceMapper;
import com.fl.nj.service.ServerServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * 服务Service实现类
 * 实现服务的业务操作
 */
@Service
public class ServerServiceServiceImpl implements ServerServiceService {
    
    @Autowired
    private ServerServiceMapper serverServiceMapper;
    
    @Override
    public boolean save(ServerService serverService) {
        // 检查服务名称在同一设备下是否已存在
        ServerService existing = serverServiceMapper.getByServiceNameAndDeviceId(
                serverService.getServiceName(), serverService.getDeviceId());
        if (existing != null) {
            return false;
        }
        return serverServiceMapper.save(serverService) > 0;
    }
    
    @Override
    public boolean update(ServerService serverService) {
        // 检查服务是否存在
        ServerService existing = serverServiceMapper.getById(serverService.getId());
        if (existing == null) {
            return false;
        }
        
        // 检查服务名称在同一设备下是否已被其他记录使用
        ServerService serviceByName = serverServiceMapper.getByServiceNameAndDeviceId(
                serverService.getServiceName(), serverService.getDeviceId());
        if (serviceByName != null && !serviceByName.getId().equals(serverService.getId())) {
            return false;
        }
        
        return serverServiceMapper.update(serverService) > 0;
    }
    
    @Override
    public boolean delete(Long id) {
        // 检查服务是否存在
        ServerService existing = serverServiceMapper.getById(id);
        if (existing == null) {
            return false;
        }
        
        return serverServiceMapper.delete(id) > 0;
    }
    
    @Override
    public ServerService getById(Long id) {
        return serverServiceMapper.getById(id);
    }
    
    @Override
    public ServerService getByServiceNameAndDeviceId(String serviceName, Long deviceId) {
        return serverServiceMapper.getByServiceNameAndDeviceId(serviceName, deviceId);
    }
    
    @Override
    public List<ServerService> getByDeviceId(Long deviceId) {
        return serverServiceMapper.getByDeviceId(deviceId);
    }
    
    @Override
    public List<ServerService> getAllWithDetails() {
        return serverServiceMapper.getAllWithDetails();
    }
    
    @Override
    public List<ServerService> getPageListWithDetails(Map<String, Object> params) {
        // 计算分页起始位置
        Integer page = (Integer) params.get("page");
        Integer pageSize = (Integer) params.get("pageSize");
        if (page != null && pageSize != null) {
            params.put("start", (page - 1) * pageSize);
        }
        return serverServiceMapper.getPageListWithDetails(params);
    }
    
    @Override
    public int getTotal(Map<String, Object> params) {
        // 添加日志以调试参数和结果
        System.out.println("ServerServiceServiceImpl.getTotal - 参数: " + params);
        int total = serverServiceMapper.getTotal(params);
        System.out.println("ServerServiceServiceImpl.getTotal - 结果: " + total);
        return total;
    }
}